#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    QStringList serialNamePort;

    ui->setupUi(this);
    this->setWindowTitle("serial_led");

    // 创建一个串口对象
    serialPort = new QSerialPort(this);

    // 搜索可用串口
    foreach (const QSerialPortInfo &inf0, QSerialPortInfo::availablePorts()) {
        serialNamePort<<inf0.portName();
    }
    ui->U_comboBox->addItems(serialNamePort);
}


Widget::~Widget()
{
    delete ui;
}


void Widget::on_open_usart_pushButton_clicked()
{
    // 串口设置
        serialPort->setPortName(ui->U_comboBox->currentText());
        serialPort->setBaudRate(ui->B_comboBox->currentText().toInt());
        serialPort->setDataBits(QSerialPort::Data8);
        serialPort->setStopBits(QSerialPort::OneStop);
        serialPort->setParity(QSerialPort::NoParity);

        //提示
        if(true == serialPort->open(QIODevice::ReadWrite))
        {
            QMessageBox::information(this,"Message","OpenSucceed");
        }
        else
        {
            QMessageBox::critical(this,"Message","OpenFailed");
        }
}

void Widget::on_open_led_pushButton_clicked()
{
    serialPort->write("R1\n");
        qDebug("ON\n");
}

void Widget::on_close_led_pushButton_clicked()
{
    serialPort->write("R0\n");
        qDebug("OFF\n");
}

void Widget::on_close_usart_pushButton_clicked()
{
    //关闭串口
        serialPort->close();
        QMessageBox::information(this,"Message","CloseSucceed");
}
